package org.icnmasjid.member.webapp.action;

import java.beans.PropertyEditorSupport;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.beanutils.BeanUtils;
import org.displaytag.pagination.PaginatedList;
import org.displaytag.properties.SortOrderEnum;

import org.icnmasjid.member.Constants;
import org.icnmasjid.member.model.Person;
import org.icnmasjid.member.model.PersonSubOrgRole;
import org.icnmasjid.member.model.Role;
import org.icnmasjid.member.model.SubOrg;
import org.icnmasjid.member.service.PersonManager;
import org.icnmasjid.member.service.RoleManager;
import org.icnmasjid.member.service.impl.BaseManager;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class PersonController extends BaseFormController {
    private final Log log = LogFactory.getLog(PersonController.class);
    private PersonManager personManager = null;
    private RoleManager roleManager = null;

	public void setPersonManager(PersonManager personManager) {
        this.personManager = personManager;
    }

	public PersonController() {
	    setCommandName("person");
	    setCommandClass(Person.class);

	}

	protected boolean isFormSubmission(HttpServletRequest arg0) {
		return true;
	}

    public ModelAndView onSubmit(HttpServletRequest request,
                                 HttpServletResponse response, Object command,
                                 BindException errors)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("entering 'onSubmit' method...");
        }
        Person person = (Person) command;
        // get the three parameters sort, dir and page
        String sort = "personId";
        if ( StringUtils.isNotBlank(request.getParameter("sort")) ) {
        	sort = request.getParameter("sort");
        }
        String dir = "asc";
        if ( StringUtils.isNotBlank(request.getParameter("dir")) ) {
        	dir = request.getParameter("dir");
        }
        int page = 1;
        if ( StringUtils.isNotBlank(request.getParameter("page")) ) {
        	page = Integer.valueOf(request.getParameter("page"));
        }

        List persons = personManager.getPersons(person, sort, dir, Integer.valueOf(page), 25);
        int fullListSize = personManager.getPersonsCount(person);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put(getCommandName(), command);
        map.put(Constants.PERSON_LIST, createPaginatedList(persons, fullListSize, 25, page, sort, dir));
    	map.put("subOrgs", getRoleManager().getSubOrgs());
    	map.put("roles", getRoleManager().getRoles(new Role()));
        return new ModelAndView("personList", map);
    }

	public RoleManager getRoleManager() {
		return roleManager;
	}

	public void setRoleManager(RoleManager roleManager) {
		this.roleManager = roleManager;
	}


}
